package com.qdy.service.impl;

import com.qdy.entity.dynamic.Dynamic;
import com.qdy.entity.dynamic.DynamicComment;
import com.qdy.entity.dynamic.DynamicLike;
import com.qdy.repository.DynamicCommentRepository;
import com.qdy.repository.DynamicLikeRepository;
import com.qdy.repository.DynamicRepository;
import com.qdy.service.DynamicService;
import com.qdy.dto.DynamicDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by qudingyi on 2017/4/22.
 */
@Service
public class DynamicServiceImpl implements DynamicService {
    @Autowired
    private DynamicRepository dynamicRepository;
    @Autowired
    private DynamicCommentRepository dynamicCommentRepository;
    @Autowired
    private DynamicLikeRepository dynamicLikeRepository;
    @Override
    public DynamicDto getDynamicInfo(Long dynamicId) {
        DynamicDto view=new DynamicDto();
        view.setDynamic(dynamicRepository.findOne(dynamicId));
        view.setComments(dynamicCommentRepository.findByDynamicIdOrderByCommentTimeDesc(dynamicId));
        view.setLikes(dynamicLikeRepository.findByDynamicIdOrderByLikeTimeDesc(dynamicId));
        return view;
    }

    @Override
    public void saveDynamic(Dynamic dynamic) {
        dynamicRepository.save(dynamic);
    }

    @Override
    public void saveLike(DynamicLike dynamicLike) {
        dynamicLikeRepository.save(dynamicLike);
    }

    @Override
    public void saveComment(DynamicComment comment) {
        dynamicCommentRepository.save(comment);
    }

    @Override
    public List<Dynamic> findByAll() {
        return dynamicRepository.findAll();
    }
}
